

package com.hazelcast.test.starter.constructor;

import com.hazelcast.test.starter.HazelcastStarterConstructor;

import java.lang.reflect.Method;

import static com.hazelcast.test.starter.HazelcastProxyFactory.proxyArgumentsIfNeeded;
import static com.hazelcast.test.starter.ReflectionUtils.getFieldValueReflectively;

@HazelcastStarterConstructor(classNames = {"com.hazelcast.instance.EndpointQualifier"})
public class EndpointQualifierConstructor extends AbstractStarterObjectConstructor {

    public EndpointQualifierConstructor(Class<?> targetClass) {
        super(targetClass);
    }

    @Override
    Object createNew0(Object delegate) throws Exception {

        ClassLoader targetClassLoader = targetClass.getClassLoader();

        Object protocolType = getFieldValueReflectively(delegate, "type");
        Object identifier = getFieldValueReflectively(delegate, "identifier");
        Class<?> protocolTypeClass = targetClassLoader.loadClass("com.hazelcast.instance.ProtocolType");

        Method resolveMethod = targetClass.getDeclaredMethod("resolve", protocolTypeClass, String.class);
        Object[] args = new Object[]{protocolType, identifier};
        Object[] proxiedArgs = proxyArgumentsIfNeeded(args, targetClassLoader);
        return resolveMethod.invoke(null, proxiedArgs);
    }
}
